<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <template v-if="!form.id">
        <a-form-model-item label="徽章" prop="badgeId" >
          <a-select placeholder="请选择徽章" v-model="form.badgeId">
            <a-select-option v-for="(d, index) in badgeList" :key="index" :value="d.id" >{{ d.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="用户" prop="userId" >
          <a-select placeholder="请搜索用户" v-model="form.userId"
                    show-search
                    :show-arrow="false"
                    :default-active-first-option="false"
                    @search="getUserList"
                    :filter-option="false">
            <a-select-option v-for="(d, index) in userList" :key="index" :value="d.id" >{{ d.nickname }}</a-select-option>
          </a-select>
        </a-form-model-item>
      </template>
      <template v-if="form.id">
        <a-form-model-item label="徽章" prop="badgeId" >
          <a-avatar :src="form.badgeImage"></a-avatar>
          {{form.badgeName}}
        </a-form-model-item>
        <a-form-model-item label="用户" prop="userId" >
          <a-avatar :src="form.userImage"></a-avatar>
          {{form.userName}}
        </a-form-model-item>
      </template>
      <a-form-model-item label="状态" prop="status" >
        <a-select placeholder="请选择状态" v-model="form.status">
          <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
  import { getUserBadge, addUserBadge, updateUserBadge,listUser } from '@/api/system/userBadge'

  export default {
    name: 'CreateForm',
    props: {
        statusOptions: {
            type: Array,
            required: true
          },
        badgeList: {
          type: Array,
          required: true
        }
    },
    components: {
    },
    data () {
      return {
        submitLoading: false,
        formTitle: '',
        userList: [],
        // 表单参数
        form: {
            id: undefined,
            badgeId: undefined,
            userId: undefined,
            status: '0',
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        },
        // 1增加,2修改
        formType: 1,
        open: false,
        rules: {
        }
      }
    },
    filters: {
    },
    created () {
    },
    computed: {
    },
    watch: {
    },
    mounted () {
    },
    methods: {
      getUserList(value){
        if (!value){
          this.userList = [];
          return
        }
        listUser({"nickname":value}).then(res => {
          this.userList = res.data
        })
      },
      onClose () {
        this.open = false
      },
      // 取消按钮
      cancel () {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset () {
        this.formType = 1
        this.form = {
            id: undefined,
            badgeId: undefined,
            userId: undefined,
            status: '0',
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
      },
      /** 新增按钮操作 */
      handleAdd (row) {
        this.reset()
        this.formType = 1
        this.open = true
        this.formTitle = '添加'
      },
      /** 修改按钮操作 */
      handleUpdate (row, ids) {
        this.reset()
        this.formType = 2
        const id = row ? row.id : ids
        getUserBadge(id).then(response => {
          this.form = response.data
          this.open = true
          this.formTitle = '修改'

          this.form.badgeName = row.badgeName
          this.form.badgeImage = row.badgeImage
          this.form.userName = row.userName
          this.form.userImage = row.userImage
          console.log(this.form)
        })
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.submitLoading = true
            if (this.form.id !== undefined && this.form.id !== null) {
              updateUserBadge(this.form).then(response => {
                this.$message.success('修改成功',3)
                this.open = false
                this.$emit('ok')
              }).finally(() => {
                this.submitLoading = false
              })
            } else {
              addUserBadge(this.form).then(response => {
                this.$message.success('新增成功',3)
                this.open = false
                this.$emit('ok')
              }).finally(() => {
                this.submitLoading = false
              })
            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>
